package cn.fl.risknew.util;

import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.vo.CapOverdueInfoVO;
import cn.fl.risknew.constant.RiskConstant;
import cn.fl.risknew.vo.RskCommRecVO;
import cn.lb.framework.poi.excel.ExcelExportUtil;
import cn.lb.framework.poi.excel.entity.TemplateExportParams;
import cn.lb.framework.poi.excel.entity.enmus.ExcelStyleType;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * Function: 导出风险管理中的数据
 * Created by Administrator on 2016/12/27.
 */
public class ExportRiskUtil {

	private static final Logger logger = LoggerFactory.getLogger(ExportRiskUtil.class);

    /**
     * 导出罚息
     * @param map
     * @return
     */
    public ByteArrayOutputStream exportFineXls(Map<String,Object> map){
        ByteArrayOutputStream baos = null;//
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
        List<CapOverdueInfoVO> voList=(List<CapOverdueInfoVO>)map.get("fineList");
        long i=0l;
        for(CapOverdueInfoVO vo:voList){
            vo.setId(++i);
            vo.setPenaltyAmountStr(nf.format(Double.valueOf(vo.getPenaltyAmount()) / 100));
            vo.setRecAmtStr(nf.format(Double.valueOf(vo.getRecAmt()) / 100));
        }
        String tempUrl= ExportRiskUtil.class.getClassLoader().getResource("/template/risk/rskFineTemp.xls").getFile();
        String sheetNm="罚息明细";
        TemplateExportParams params = new TemplateExportParams(tempUrl, false, sheetNm);
        params.setHeadingRows(1);
        params.setHeadingStartRow(1);
        params.setStyle(ExcelStyleType.BORDER.getClazz());
        params.setColForEach(true);//注：包含遍历list
        try {
            Workbook book = ExcelExportUtil.exportExcel(params, map);
            baos = new ByteArrayOutputStream();
            book.write(baos);
        } catch (IOException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
        }
        return baos;
    }





    /**
     * 导出逾期催收、租金提醒沟通记录
     *
     * @param map
     * @return
     */
    public ByteArrayOutputStream exportCommRecXls(Map<String, Object> map) {
        ByteArrayOutputStream baos = null;//¥
        List<RskCommRecVO> voList = (List<RskCommRecVO>) map.get("commRecList");
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
        long i=0l;
        for(RskCommRecVO vo:voList){
            vo.setId(++i);//序号
            if(StringUtils.isNotEmpty(vo.getCollectCost())){
                vo.setCollectCosts(nf.format(Double.valueOf(vo.getCollectCost()) / 100));
            }
            if(null!=vo.getCollectTm()){
                vo.setCollectTms(DateUtils.formatDateToString(vo.getCollectTm(),DateUtils.YYYY_MM_DD));
            }
        }
        map.put("commRecList",voList);
        String tempUrl="";
        String sheetNm="";
        if(StringUtils.isNotBlank((String) map.get("buTypCd"))&&map.get("buTypCd").equals(RiskConstant.CODE_BU_TYP_CD_1)){
            tempUrl= ExportRiskUtil.class.getClassLoader().getResource("/template/risk/ovdCollectTemp.xls").getFile();
            sheetNm="逾期催收记录";
        }else if(StringUtils.isNotBlank((String) map.get("buTypCd"))&&map.get("buTypCd").equals(RiskConstant.CODE_BU_TYP_CD_2)){
            tempUrl= ExportRiskUtil.class.getClassLoader().getResource("/template/risk/rentRemindTemp.xls").getFile();
            sheetNm="租金提醒记录";
        }else{
            BaseLogUtils.info(logger,"exportCommRecXls","导出逾期催收、租金提醒沟通记录失败，未找到导出文件模板");
            return null;
        }

        TemplateExportParams params = new TemplateExportParams(tempUrl, false, sheetNm);
        params.setHeadingRows(1);
        params.setHeadingStartRow(1);
        params.setStyle(ExcelStyleType.BORDER.getClazz());
        params.setColForEach(true);//注：包含遍历list
        try {
            Workbook book = ExcelExportUtil.exportExcel(params, map);
            baos = new ByteArrayOutputStream();
            book.write(baos);
        } catch (IOException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
        }
        return baos;
    }
}
